<?php
namespace app\serveruser\logic;

use \app\manage\logic\BaseLogic;

/**
 * 应用管理的逻辑层
 * @author adv30 <adv30@126.com> 2017年6月1日9:50:24
 */
class App extends BaseLogic
{
    private $appModel = 'app\serveruser\model\App';
    // 增加一个应用
    public function add(array $data): array
    {
        $data['app_key'] = md5($data['name'] . NOW . mt_rand());
        $data['status'] = '1';
        $allowRoleIds = explode(',', $data['allow_role_ids']);
        $allowRoleIds = array_unique($allowRoleIds);
        $data['allow_role_ids'] = implode(',', $allowRoleIds);
        try{
            $res = $this->model($this->appModel)->save($data);
        }catch (\Exception $e) {
            // 当唯一键重复等报错时
            return server_err(null, '数据库异常:' . $e->getMessage());
        }
        return auto_return($res, '增加应用成功', '增加应用失败');
    }
    // 停止一个应用砌筑
    public function stop(array $data): array
    {
        $_where = make_map($data, ['id', 'name', 'app_key']);
        $save['status'] = '0';
        $res = $this->model($this->appModel)->where($_where)->update($save);
        return auto_return($res, '停止应用成功', '停止应用失败');
    }
    // 获取指定分页数量的应用列表
    public function list(array $data): array
    {
        $_where = isset($data['status']) ? ['status' => $data['status']] : null;
        $res = $this->model($this->appModel)
                    ->where($_where)
                    ->paginate($data['list_rows'], false, ['page'=>$data['page']])
                    ->toArray();
        return auto_return_page($res);
    }
    // 查找一个应用信息
    public function find(array $data): array
    {
        $_where = make_map($data);
        $res = $this->model($this->appModel)
                    ->where($_where)
                    ->find();
        return auto_return($res, '成功找到应用信息', '没有找到应用信息');
    }
    // 编辑一个应用
    public function edit(array $data): array
    {
        $_where['id'] = $data['id'];
        $save = make_map($data, ['name', 'allow_role_ids']);
        $res = $this->model($this->appModel)->save($save, $_where);
        return auto_return($res, '编辑应用成功', '编辑应用失败');
    }
    // 为应用增加或减少某一角色
    private function editRolesForApp(array $data, string $type): array
    {
        $_where = make_map($data, ['id', 'name']);
        $findInfo = $this->find($_where);
        // 先查找原来的角色ID
        if ($findInfo['status'] !== SUCCESS_STATUS) {
            return $findInfo;
        }
        $oldRoleIds = explode(',', $findInfo['res']['allow_role_ids']);
        $newRoleIds = explode(',', $data['allow_role_ids']);
        if ($type === 'add') {
            $newRoleIds = array_merge($oldRoleIds, $newRoleIds);
        } else {
            $newRoleIds = array_diff($oldRoleIds, $newRoleIds);
        }

        $newRoleIds = array_unique($newRoleIds);
        sort($newRoleIds);
        $save['allow_role_ids'] = implode(',', $newRoleIds);
        $res = $this->model($this->appModel)->save($save, $_where);
        return auto_return($res, '编辑应用成功', '编辑应用失败');
    }
    // 为某一应用增加允许访问角色
    public function addRoles(array $data): array
    {
        return $this->editRolesForApp($data, 'add');
    }
    // 为某一应用移除允许访问的角色
    public function removeRoles(array $data): array
    {
        return $this->editRolesForApp($data, 'remove');
    }
    // 启用某一应用的信息
    public function start(array $data): array
    {
        $_where = make_map($data, ['id', 'name', 'app_key']);
        $save['status'] = '1';
        $res = $this->model($this->appModel)->where($_where)->update($save);
        return auto_return($res, '启用应用成功', '启用应用失败');
    }
    // 查看所有应用不分页
    public function all(array $data): array
    {
        $_where['status'] = '1';
        $res = $this->model($this->appModel)
                    ->where($_where)
                    ->select();
        return auto_return($res, '成功获取所有应用', '获取所有应用失败');
    }
    // 根据app_ids获取app_name
    public function getAppNameByIds(array $data): array
    {
        $_where['status'] = '1';
        $_where['id'] = ['in', $data['app_ids']];
        $res = $this->model($this->appModel)
                    ->where($_where)
                    ->column('id,name');
        return auto_return($res, '成功根据app_ids获取app_name', '根据app_ids获取app_name失败');
    }
}
